//------------------------------------------------------------------------
// <copyright file="MetaData.cs" company="DevDir">
//   Copyleft (c) DevDir. No rights reserved.
// </copyright>
// <author>Nikita Sharov</author>
//------------------------------------------------------------------------

namespace DevDir.Mp3
{
  using System;
  using System.Data;
  
  /// <summary>
  /// Relevant metadata.
  /// </summary>
  public class MetaData
  {
    private string artist;
    private uint? year;
    private string album;
    private uint? track;
    private uint? trackCount;
    private uint? disc;
    private uint? discCount;
    private string title;
    private TimeSpan? duration;
    private string genre;

    public string Artist
    {
      get { return this.artist; }
      set { this.artist = value; }
    }

    public uint? Year
    {
      get { return this.year; }
      set { this.year = value; }
    }

    public string Album
    {
      get { return this.album; }
      set { this.album = value; }
    }

    public uint? Track
    {
      get { return this.track; }
      set { this.track = value; }
    }

    public uint? TrackCount
    {
      get { return this.trackCount; }
      set { this.trackCount = value; }
    }

    public uint? Disc
    {
      get { return this.disc; }
      set { this.disc = value; }
    }

    public uint? DiscCount
    {
      get { return this.discCount; }
      set { this.discCount = value; }
    }

    public string Title
    {
      get { return this.title; }
      set { this.title = value; }
    }

    public TimeSpan? Duration
    {
      get { return this.duration; }
      set { this.duration = value; }
    }

    public string Genre
    {
      get { return this.genre; }
      set { this.genre = value; }
    }

    public static MetaData Create(TagLib.File file)
    {
      MetaData md = new MetaData();
      if (file.Tag.FirstPerformer != null)
      {
        if (file.Tag.FirstPerformer != String.Empty)
        {
          md.artist = file.Tag.FirstPerformer;
        }
      }

      if (file.Tag.Year != 0)
      {
        md.year = file.Tag.Year;
      }

      if (file.Tag.Album != null)
      {
        if (file.Tag.Album != String.Empty)
        {
          md.album = file.Tag.Album;
        }
      }

      if (file.Tag.Track != 0)
      {
        md.track = file.Tag.Track;
      }

      if (file.Tag.TrackCount != 0)
      {
        md.trackCount = file.Tag.TrackCount;
      }

      if (file.Tag.Disc != 0)
      {
        md.disc = file.Tag.Disc;
      }

      if (file.Tag.DiscCount != 0)
      {
        md.discCount = file.Tag.DiscCount;
      }

      if (file.Tag.Title != null)
      {
        if (file.Tag.Title != String.Empty)
        {
          md.title = file.Tag.Title;
        }
      }

      md.duration = new TimeSpan(
        file.Properties.Duration.Hours, 
        file.Properties.Duration.Minutes, 
        file.Properties.Duration.Seconds);

      if (file.Tag.FirstGenre != null)
      {
        if (file.Tag.FirstGenre != String.Empty)
        {
          md.genre = file.Tag.FirstGenre;
        }
      }

      return md;
    }
  }
}
